﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;

namespace Custom.Sequential.Approval.Workflow
{
    public sealed partial class CustomApproval : SequentialWorkflowActivity
    {
        public CustomApproval()
        {
            InitializeComponent();
        }
        #region ALL Code
        public Guid workflowId = default(System.Guid);
        public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties();
        public Guid taskID = Guid.NewGuid();
        private bool bTaskComplete = false;
        public bool bIfElse = false;
        public enum EventType
        {
            Intitialize,
            Approved,
            Reject
        }

        #region Code Conditions
        private void completed(object sender, ConditionalEventArgs e)
        {
            e.Result = bTaskComplete;
        }
        private void notCompleted(object sender, ConditionalEventArgs e)
        {
            e.Result = !bTaskComplete;
        }
        private void IfElseConditionApproved(object sender, ConditionalEventArgs e)
        {
            e.Result = bIfElse;
        }
        private void IfElseConditionReject(object sender, ConditionalEventArgs e)
        {
            e.Result = !bIfElse;
        }
        #endregion

        private void UpdateListItem(EventType eEvent)
        {
            try
            {
                SPListItem ltItem = workflowProperties.Item;

                
                switch (eEvent)
                {
                    case EventType.Intitialize:
                       //Update List Item field
                        break;
                    case EventType.Approved:
                        //Update List Item field
                        break;
                    case EventType.Reject:
                        //Update List Item field
                        break;
                }

                //update item being put through the workflow
                ltItem.SystemUpdate(false);
            }
            catch (Exception ex)
            {
            }
        }

        public static bool IsTaskApproved(SPWorkflowTaskProperties TaskProperties)
        {
            return TaskProperties.ExtendedProperties["TaskStatus"] != null
                && TaskProperties.ExtendedProperties["TaskStatus"] as string == "#";
        }

        private void WorkflowItemDelete_Invoked(object sender, ExternalDataEventArgs e)
        {
            bTaskComplete = true;
        }

        #region Task Update

        private void onTaskChanged1_Invoked(object sender, ExternalDataEventArgs e)
        {
            SPTaskServiceEventArgs args = (SPTaskServiceEventArgs)e;

            if (IsTaskApproved(args.afterProperties))
            {
                UpdateListItem(EventType.Approved);
                bIfElse = true;
            }
            else
            {
                UpdateListItem(EventType.Reject);
                bIfElse = false;
            }
            bTaskComplete = true;
        }

        private void TaskDelete_Invoked(object sender, ExternalDataEventArgs e)
        {
            bTaskComplete = true;
        }

        private void createTask1_MethodInvoking(object sender, EventArgs e)
        {

            CreateTask task = sender as CreateTask;
            task.TaskId = taskID;
            SPWorkflowTaskProperties wtp = new SPWorkflowTaskProperties();
            wtp.PercentComplete = (float)0.0;
            wtp.DueDate = DateTime.Now.AddDays(30);
            if (String.IsNullOrEmpty(workflowProperties.Item.Title))
            {
                wtp.Title = "Please Review: " + workflowProperties.Item.Name;
            }
            else
            {
                wtp.Title = "Please Review: " + workflowProperties.Item.Title;
            }

            //change as per group configure by user
            wtp.AssignedTo = "administrator";
            wtp.TaskType = 1;
            wtp.ExtendedProperties["ows_AllowDelegation"] = "False";
            wtp.ExtendedProperties["ows_AllowChangeRequests"] = "False";

            task.TaskProperties = wtp;


            UpdateListItem(EventType.Intitialize);
        }


        #endregion

        #region SendMail


        private void SendMail()
        {
       
        }

        private void SendMail_ExecuteCode(object sender, EventArgs e)
        {
            bTaskComplete = true;
        }

        private void SendMailAfterTaskCreation_ExecuteCode(object sender, EventArgs e)
        {

        }

        private void SendMailAfterTaskDelete_ExecuteCode(object sender, EventArgs e)
        {
            bTaskComplete = true;
        }

        private void SendMailAfterRejection_ExecuteCode(object sender, EventArgs e)
        {
            bTaskComplete = true;
        }

        private void SendMailAfterWorkflowComplete_ExecuteCode(object sender, EventArgs e)
        {
            bTaskComplete = true;
        }
        #endregion
        #endregion
    }
}
